package com.ssyt.examinationsystem.controller;

import com.alibaba.fastjson.JSON;
import com.ssyt.examinationsystem.entity.ChoiceInfo;
import com.ssyt.examinationsystem.entity.CourseInfo;
import com.ssyt.examinationsystem.entity.PageBean;
import com.ssyt.examinationsystem.service.IChoiceInfoService;
import com.ssyt.examinationsystem.service.impl.ChoiceInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class ChoiceInfoController
 */
public class ChoiceInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChoiceInfoController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用选择题的业务对象
        IChoiceInfoService choiceService = new ChoiceInfoServiceImpl();

        PrintWriter out = response.getWriter();
        // 获取地址栏上的flag值
        String flag = request.getParameter("flag");
        if ("query".equals(flag)) {
            query(request, response);
        } else if ("single".equals(flag)) {
            single(choiceService, request, response);
        } else if ("multiple".equals(flag)) {
            multiple(choiceService, request, response);
        } else if ("list".equals(flag)) {
            list(choiceService, request, response, out);
        } else if ("add".equals(flag)) {
            add(choiceService, request, response, out);
        } else if ("dele".equals(flag)) {
            delete(choiceService, request, response, out);
        } else if ("update".equals(flag)) {
            update(choiceService, request, response, out);
        }

        out.flush();
        out.close();

    }

    /**
     * 修改选择题
     *
     * @param choiceService
     * @param request
     * @param response
     * @param out
     */
    private void update(IChoiceInfoService choiceService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取表单数据
        String choiceId = request.getParameter("choiceId");
        String question = request.getParameter("question");
        String answerA = request.getParameter("answerA");
        String answerB = request.getParameter("answerB");
        String answerC = request.getParameter("answerC");
        String answerD = request.getParameter("answerD");
        String answer = request.getParameter("answer");
        String choiceType = request.getParameter("choiceType");
        String courseId = request.getParameter("courseId");
        String remark = request.getParameter("remark");
        // 创建一个选择题对象，并封装属性
        ChoiceInfo choice = new ChoiceInfo();
        choice.setQuestion(question);
        choice.setAnswerA(answerA);
        choice.setAnswerB(answerB);
        choice.setAnswerC(answerC);
        choice.setAnswerD(answerD);
        choice.setAnswer(answer);
        choice.setChoiceType(Integer.valueOf("".equals(choiceType) ? "0" : choiceType));
        CourseInfo course = new CourseInfo();
        course.setCourseId(Integer.valueOf("".equals(courseId) ? "0" : courseId));
        choice.setCourse(course);
        choice.setRemark(remark);
        choice.setChoiceId(Integer.valueOf(choiceId));
        // 调用删除业务方法
        int row = choiceService.updateChoice(choice);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 根据编号删除选择题
     *
     * @param choiceService
     * @param request
     * @param response
     * @param out
     */
    private void delete(IChoiceInfoService choiceService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取表单数据
        String choiceId = request.getParameter("choiceId");
        int row = choiceService.deleteChoice(choiceId);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 添加选择题
     *
     * @param choiceService
     * @param request
     * @param response
     * @param out
     */
    private void add(IChoiceInfoService choiceService, HttpServletRequest request, HttpServletResponse response,
                     PrintWriter out) {
        // 获取表单数据
        String question = request.getParameter("question");
        String answerA = request.getParameter("answerA");
        String answerB = request.getParameter("answerB");
        String answerC = request.getParameter("answerC");
        String answerD = request.getParameter("answerD");
        String answer = request.getParameter("answer");
        String choiceType = request.getParameter("choiceType");
        String courseId = request.getParameter("courseId");
        String remark = request.getParameter("remark");
        // 创建一个选择题对象，并封装属性
        ChoiceInfo choice = new ChoiceInfo();
        choice.setQuestion(question);
        choice.setAnswerA(answerA);
        choice.setAnswerB(answerB);
        choice.setAnswerC(answerC);
        choice.setAnswerD(answerD);
        choice.setAnswer(answer);
        choice.setChoiceType(Integer.valueOf("".equals(choiceType) ? "0" : choiceType));
        CourseInfo course = new CourseInfo();
        course.setCourseId(Integer.valueOf("".equals(courseId) ? "0" : courseId));
        choice.setCourse(course);
        choice.setRemark(remark);
        // 调用添加的业务方法
        int row = choiceService.addChoiceInfo(choice);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 分页查询所有选择题
     *
     * @param choiceService
     * @param request
     * @param response
     * @param out
     */
    private void list(IChoiceInfoService choiceService, HttpServletRequest request, HttpServletResponse response,
                      PrintWriter out) {
        PageBean page = new PageBean();
        // 获取当前页
        String current = request.getParameter("page");
        // 获取每页显示数
        String pageSize = request.getParameter("rows");
        // 获取页面传入当前页
        if (current != null) {
            Integer currentPage = Integer.valueOf(current);
            page.setCurrentPage(currentPage);
            page.setPageSize(Integer.valueOf(pageSize));
        }

        PageBean pageBean = choiceService.getChoiceByPage(page);

        Map<String, Object> jsonData = new HashMap<String, Object>();
        jsonData.put("total", pageBean.getTotalRecords());
        jsonData.put("rows", pageBean.getList());
        String jsonDataString = JSON.toJSONString(jsonData, true);

        // 调用查询所有选择题的业务方法
        // List<Map<String, Object>> choiceList = choiceService.getChoice();
        // 将集合转换位json字符串
        // String json = JSON.toJSONString(choiceList, true);
        // 输出json字符串
        out.print(jsonDataString);
    }

    /**
     * 显示所有多选题到选题窗口
     *
     * @param choiceService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void multiple(IChoiceInfoService choiceService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Map<String, Object>> multiples = choiceService.getMultiple();
        request.setAttribute("multiples", multiples);
        // 转发到选题页面
        request.getRequestDispatcher("WEB-INF/views/group/selectQuestion.jsp").forward(request, response);
    }

    /**
     * 显示所有单选题到选题窗口
     *
     * @param choiceService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void single(IChoiceInfoService choiceService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Map<String, Object>> singles = choiceService.getSingles();
        request.setAttribute("singles", singles);
        // 转发到选题页面
        request.getRequestDispatcher("WEB-INF/views/group/selectQuestion.jsp").forward(request, response);
    }

    private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 转发到选择题显示页面
        request.getRequestDispatcher("WEB-INF/views/question/choice.jsp").forward(request, response);
    }

}
